package com.ax.api.route.base.common;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.ax.api.route.util.RouteConfig;

@RestController
@RequestMapping("/subject")
public class CommonRoute {

	 @Autowired
	 LoadBalancerClient loadBalancerClient;
	 @Autowired
	 RestTemplate restTemplate;
	 
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping("/sys/subject")
	String drugAdministration(Model model) {
		String url = geturl("/sys/subject");
		return restTemplate.getForObject(url, String.class);
	}
	
	/**
	 * queryBaseSubject
	 * remote_bussiness
	 * 医学科目(模块:医学科目)
	 */  
	@GetMapping("/queryBaseSubject")
	String queryBaseSubject(@RequestParam Map<String,Object> map) {
		String value = "?subjectId={subjectId}";
		String url = geturl("/sys/subject/queryBaseSubject") + value;
		return restTemplate.getForObject(url, String.class,map);
	}
	
	/**
	 * add
	 * remote_base
	 * 医学科目(模块:医学科目)*跳转增加页面
	 */   
	@GetMapping("/add/subject")
	public String addDrugAdministration(String pId){
		String value = "/pId="+pId;
		String url = geturl("/sys/subject/add")+value;
		return restTemplate.getForObject(url, String.class,pId);
  }
	
	/**
	 * insertBaseSubject
	 * remote_base
	 * 医学科目(模块:医学科目)
	 */ 
	@GetMapping("/insertBaseSubject")
	public String insertDrugDep(@RequestParam Map<String,Object> map){
		String value = "?subjectId={subjectId}&subjectCode={subjectCode}&subjectName={subjectName}&firstLetter={firstLetter}&parentSubjectId={parentSubjectId}&remarks={remarks}&flag={flag}";
		String url = geturl("/sys/subject/insertBaseSubject")+value;
		return restTemplate.getForObject(url, String.class,map);
  }
	
	/**
	 * edit
	 * remote_base
	 * 医学科目(模块:医学科目)*跳转修改页面
	 */   
	@GetMapping("/edit/subject")
	public String edit(String id){
		String value = "/id="+id;
		String url = geturl("/sys/subject/edit")+value;
		return restTemplate.getForObject(url, String.class,id);
  }
	
	/**
	 * updateBaseSubject
	 * remote_base
	 * 医学科目(模块:医学科目)
	 */  
	@GetMapping("/updateBaseSubject")
	public String updateDrugDep(@RequestParam Map<String,Object> map){
		String value = "?subjectId={subjectId}&subjectCode={subjectCode}&subjectName={subjectName}&firstLetter={firstLetter}&parentSubjectId={parentSubjectId}&remarks={remarks}&flag={flag}";
		String url = geturl("/sys/subject/updateBaseSubject")+value;
		return restTemplate.getForObject(url,String.class,map);
  }
	
	/**
	 * querySysDict
	 * remote_base
	 * 字典查询(模块:字典)
	 */  
	@GetMapping("/querySysDict")
	String querySysDict(@RequestParam Map<String,Object> map) {
		String value = "?dictId={dictId}&offset={offset}&limit={limit}";
		String url = geturl("/sys/subject/querySysDict") + value;
		return restTemplate.getForObject(url, String.class,map);
	}
	
	private String geturl(String path){
		 ServiceInstance serviceInstance = loadBalancerClient.choose(RouteConfig.BUSSINESS_ROUTE);
		 StringBuffer bf = new StringBuffer("http://");
		 bf.append(serviceInstance.getHost());
		 bf.append(":");
		 bf.append(serviceInstance.getPort());
		 bf.append(path);
		 return bf.toString();
	   }
}
